﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
namespace WPFExcelReport
{
    public partial class TEKKOPF 
    {

        public TEKKOPF()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VEKKOPF();
            this.DataContext = MV;
            MESSAGES += "EKDEL;UPDATEEK04BYEKP;";
            InitializeComponent();
        }


        public override void OnWorkflowIDChange(ReflectionStudio.Core.Events.MessageEventArgs e)
        {
            base.OnWorkflowIDChange(e);
            MessageInfo msg = e.Info;
            string task = msg.MSGTYPE;
            switch (task)
            {

                case "EKDEL":
                    EKDEL(msg);
                    break;
                case "UPDATEEK04BYEKP":
                    UPDATEEK04BYEKP(msg);
                    break;
            }
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            KEY = ""; 
            AUF = "";
            if (dwMaster == null)
                return; 
            dwEKK = dwMaster;
            if (TBMain.Columns.Contains("EKPAUFPOS"))
            {
                KEY = dwMaster["EKKAUFKRS"].ToString() + dwMaster["EKKAUFLFD"].ToString() + dwMaster["EKPAUFPOS"].ToString() + dwMaster["EKKLIEFNR"].ToString();
                AUF = "B" + KEY;
            }
            else
            {
                KEY = dwMaster["EKKAUFKRS"].ToString() + dwMaster["EKKAUFLFD"].ToString() + "000" + dwMaster["EKKLIEFNR"].ToString();
                AUF = "B" + KEY;                
            }
            dwMaster=TBMain.Rows[0];
            if (TMV.EKKEINSTA.ToString().Trim().Length == 0)
                IsAUFConirmed = false;
            else
            {
                IsAUFConirmed = true;
            }
        }

        public void UPDATEEK04BYEKP(MessageInfo msg)
        {
            if (dwEKK == null)
                return;
            dwEKK["EKKME1"] = Convert.ToDouble(msg.P1);
            dwEKK["EKKWE1"] = Convert.ToDouble(msg.P2);
            Save();
        }

        public void EKDEL(MessageInfo msg)
        {
            if (msg.LFD.Trim().Length == 0)
            {
                TBMain.Clear();
                return;
            }
            DataRow dwData = TBMain.NewRow();
            dwData["EKKMANDANT"] = app.Mandant;
            dwData["EKKAUFKRS"] = msg.KRS;
            dwData["EKKAUFLFD"] = msg.LFD;
            ((VEKKOPF)MV).DeleteEKKOPF(dwEKK, 1);

        }

        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            IsAUFConirmed = false;
            TBMain.Clear();
            dwData = TMV.VEKP.TBMain.NewRow();
            
            if (LFD == "000000")
            {
                LFD = "";
            }
            if (LFD.Trim() == "")
            {
                MessageBox.Show("请先输入采购单号才可以查询！");
                return;
            }
            else
            {
                LFD = Helpers.Helper.Right("000000" + LFD.Trim(), 6);
            }
            if (POS == "000")
            {
                POS = "";
            }

            dwData["EKPMANDANT"] = app.Mandant;
            dwData["EKPAUFKRS"] = "AND EKPAUFKRS LIKE '%" + KRS.Trim() + "%'";
            dwData["EKPAUFLFD"] = "AND EKPAUFLFD = '" + LFD.Trim() + "'";
            dwData["EKPAUFPOS"] = "AND EKPAUFPOS LIKE '%" + POS.Trim() + "%'";
            dwData["EKPLIEFNR"] = "AND EKPLIEFNR LIKE '" + LIEFNR.Trim() + "%'";
            SResult = TMV.VEKP.QEK09(dwData);
            MergeData(SResult, true); 
            base.QueryCommand(sender, e);
        }

        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            
            if (!IsNew)
            {
                TBMain.Clear();
                IsNew = true;
                LIEFNR = "";
                AUFART = "";
                return;
            }
            if (LIEFNR.Trim().Length < 6)
            {
                MessageBox.Show("请选择供应商");
                return;
            }
            if (KRS.Trim().Length < 2)
            {
                MessageBox.Show("必须输入两位长度的前缀");
                return;
            }
            IsAUFConirmed = false;
            dwData = TBMain.NewRow();
            ((VEKKOPF)MV).SetUpNewRow(ref dwData, null);
            dwData["EKKMANDANT"] = app.Mandant;
            LFD = VSDDAT.SD22.GETNEXT("0022", KRS, "EKAUFTR");
            dwData["EKKLIEFNR"] = LIEFNR;
            dwData["EKKAUFKRS"] = KRS;
            dwData["EKKAUFLFD"] = LFD;
            dwData["EKKAUFART"] = AUFART;
            if (TBMain.Columns.Contains("EKPAUFPOS"))
            {
                dwData["EKKAUFKRS"] = KRS;
                dwData["EKKAUFLFD"] = LFD;
                dwData["EKPAUFPOS"] = "000";
            }
            ((VEKKOPF)MV).Insert(dwData);
            vwMaster.View.MoveCurrentToFirst();
            IsNew = false;
            base.NewCommand(sender, e);
        }

        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwEKK == null)
                return;
            ((VEKKOPF)MV).DeleteEKKOPF(dwEKK, 1);
            base.DeleteCommand(sender, e);
        }

        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwMaster == null)
                    return;
                
                TMV.UpdateEKKOPF(dwMaster, 0, app.company);
                TMV.MANAGEEKP(dwMaster);
                base.SaveCommand(sender, e);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwMaster == null)
                    return;
                base.EditCommand(sender, e);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {

            DataSet Set = new DataSet();
            cDR.DocType = PRTYPE; 

            dwData = TMV.TBMain.Rows[0];
            dwData["EKKMANDANT"] = app.Mandant;
            string TYPE = dwData["EKKAUFART"].ToString().Trim();
            if (app.company=="TST")
            {
                 Set.Merge(TMV.QEKK05TST(dwData));
            }
            else { Set.Merge(TMV.QEKK05(dwData)); }    
            dwData = VEKPOS.Instance.TBMain.NewRow() ;
            dwData["EKPMANDANT"] = app.Mandant;
            dwData["EKPAUFKRS"] = KRS;
            dwData["EKPAUFLFD"] = LFD;
            Set.Merge(VEKPOS.Instance.QEK13(dwData));
            if(TYPE=="TW"||TYPE=="T")
            {
                cDR.DocType = "TSTEKSMP";
            }
            else
            {
                cDR.DocType = "TSTEK05";
            }
            cDR.PRINT(Set);
        }


        private void LinkedOrderCommand(object sender, RoutedEventArgs e)
        {
            VICC_WFEVENTS.SVWF.VK02(this.RUNID);
            MessageInfo msg = new MessageInfo();
            msg.MSGTYPE = "VKKLOAD";
            msg.Sender = this.RUNID;
            msg.ACTION = "VKKLOAD";
            msg.AUFTYP = "K";
            msg.KRS = dwEKK["EKKAUFKRS"].ToString();
            msg.LFD = dwEKK["EKKAUFLFD"].ToString();
            VICC_WFEVENTS.SVWF.CheckEvents("UIMESSAGE", null, "", msg);
        }

        public void EKKConfonfirm(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwMaster == null)
                    return;
                PM["ORDERNUM"] = KEY;
                PM["KRS"] = dwMaster["EKKAUFKRS"];
                PM["LFD"] = dwMaster["EKKAUFLFD"];
                PM["AUFTYP"] = "B";
                ((VEKKOPF)MV).VEKP.REMOVE0QTY(PM);
                ((VEKKOPF)MV).VEKP.LOCKEKPOS(PM);
                //TMV.VVWD.WDNEWBYEKK(PM);
                ((VEKKOPF)MV).CHECKEKKVR(dwMaster, RENR);
                dwMaster["EKKMANDANT"] = "00";
                dwMaster["EKKGESSTA"] = "21";//20是初始值
                dwMaster["EKKEINSTA"] = "10";//'  '是初始值
                IsAUFConirmed = true;
                ((VEKKOPF)MV).UEKK22(dwMaster);
                VICC_WFEVENTS.SVWF.EKVR02(RUNID, AUF);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void QueryByVKAUF(object sender, RoutedEventArgs e)
        {
            dwData = TMV.MUX.TBMain.NewRow();
            dwData["ABAMANDANT"] = app.Mandant;
            dwData["ABAVKAUFKRS"] = VKKRS;
            dwData["ABAVKAUFLFD"] = VKLFD;
            SResult = TMV.VEKP.QueryEKPOS(dwData, 10);
            MergeData(SResult, true);
            base.QueryCommand(sender, e);
        }

        public override void LOADBYAUF()
        {
            if (!IsSaved)
                return;
            if (ORDERNUM != AUF)
                return;
            if (AUF.Length == 0)
                return;

            dwData = TMV.VEKP.TBMain.NewRow();
            if (LFD == "000000")
            {
                LFD = "";
            }
            if (POS == "000")
            {
                POS = "";
            }
            dwData["EKPMANDANT"] = app.Mandant;
            dwData["EKPAUFKRS"] = "AND EKPAUFKRS= '" + AUF.Substring(1, 2) + "'";
            dwData["EKPAUFLFD"] = "AND EKPAUFLFD= '" + AUF.Substring(3, 6) + "'";
            dwData["EKPAUFPOS"] = "AND EKPAUFPOS= '" + AUF.Substring(9, 3) + "'";
            dwData["EKPLIEFNR"] = "AND EKPLIEFNR= '" + AUF.Substring(12, 8) + "'";
            SResult = TMV.VEKP.QueryEKPOS(dwData, 9);
            TBTemp = TBMain.Clone();
            TBTemp.Merge(SResult.Tables[0]);
            foreach (DataRow row in TBTemp.Rows)
            {
                dwMaster.ItemArray = row.ItemArray;
            }
            base.LOADBYAUF();
        }
    }
}
